﻿///<reference path="_references.ts" />
///<reference path="GameStateProvider.ts" />

module ScrambledEggs {
    export class Cooldowns {
        private static _gameState: GameStateProvider;
        private static _locked: bool;

        public static AutoResolve(gameState: GameStateProvider): void {
            this._gameState = gameState;

            // initialize disabled for sure!
            for (var selector in gameState.State.Cooldowns)
                this.Initialize(selector);

            setInterval(function () {
                var me = this;
                for (var selector in gameState.State.Cooldowns) {
                    var cooldown = --gameState.State.Cooldowns[selector];

                    if (me._locked)
                        return;

                    $(selector).each(function () {
                        var $el = $(this);

                        var origHtml = $el.attr("data-orightml");
                        if (cooldown == 0) {
                            $el.html(origHtml).removeAttr("disabled").removeAttr("data-orightml");
                            delete gameState.State.Cooldowns[selector];
                            return;
                        }

                        $el.html(origHtml + " (" + cooldown + ")");
                    });
                }
            }, 1000);
        }
        public static Set(selector: string, duration: number = 5)
        {
            this._gameState.State.Cooldowns[selector] = duration;
            this.Initialize(selector);
        }
        public static SetIfGreater(selector: string, duration: number = 5) {
            var cd = this._gameState.State.Cooldowns[selector];
            if(cd == null || duration > cd)
                this._gameState.State.Cooldowns[selector] = duration;

            if(cd == null)
                this.Initialize(selector);
        }
        public static Increase(selector: string, duration: number = 5) {
            var cd = this._gameState.State.Cooldowns[selector];
            this._gameState.State.Cooldowns[selector] = (cd == null ? duration : (cd + duration));

            if(cd == null)
                this.Initialize(selector);
        }
        public static Decrease(selector: string, duration: number = 5) {
            var cd = this._gameState.State.Cooldowns[selector];
            this._gameState.State.Cooldowns[selector] = (cd == null ? 0 : (Math.max(cd - duration, 0)));

            if(cd == null)
                this.Initialize(selector);
        }
        public static Lock() {
            this._locked = true;
            $("button").attr("disabled", "disabled");
        }
        public static Unlock() {
            if (!this._locked)
                return;

            this._locked = false;
            $("button").removeAttr("disabled");
            for (var selector in this._gameState.State.Cooldowns)
                this.Initialize(selector);
        }
        private static Initialize(selector: string) {
            var me = this;
            $(selector).each(function () {
                var $el = $(this).attr("disabled", "disabled");
                var origHtml = $el.attr("data-orightml");
                if (origHtml == null)
                    $el.attr("data-orightml", origHtml = $el.html());
                if(!me._locked)
                    $el.html(origHtml + " (" + me._gameState.State.Cooldowns[selector] + ")");
            });
        }
    }
}